package com.app.service;

import com.app.dao.StaffDao;
import com.app.entity.ResidentialWorker;
import com.app.entity.User;
import com.app.exception.ValidateException;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.ValidationException;
import java.util.List;

/**
 * Created by xinjian.hu on 18-3-22.
 */
@Service
public class StaffService {
    @Autowired
    private StaffDao staffDao;

    /**
     * 获取员工列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     * @throws ValidationException
     */
    public ReturnResultResponseVO<ResidentialWorker> getStaffList(int currentPage,
                                                                  int pageSize,
                                                                  String workerType,
                                                                  String workerName) throws ValidationException {

        ReturnResultResponseVO<ResidentialWorker> responseData = new ReturnResultResponseVO<ResidentialWorker>();
        int countPage = 0;
        int count = staffDao.getCount(workerType, workerName);
        try {
            if (count % pageSize == 0) {
                countPage = count / pageSize;
            } else {
                countPage = count / pageSize + 1;
            }
            if (currentPage < 1) {
                currentPage = 1;
            }
            if(countPage == 0) {
                countPage = 1;
            }
            if (currentPage > countPage) {
                currentPage = countPage;
            }
            PageVO pageVO = new PageVO();
            pageVO.setCountPage(countPage);
            pageVO.setCurrentPage(currentPage);
            pageVO.setPagesize(pageSize);
            responseData.setPage(pageVO);
            List<ResidentialWorker> residentialWorkers = staffDao.queryStaffList((currentPage - 1) * pageSize, pageSize, workerType, workerName);
            responseData.setData(residentialWorkers);
        } catch (Exception e) {
            throw new ValidateException("获取用员工列表异常");
        }
        return responseData;
    }

    /**
     * 获取员工详情
     *
     * @param workerId
     * @return
     */
    public ResidentialWorker staffDetail(int workerId) {

        ResidentialWorker residentialWorker = new ResidentialWorker();
        try {
            residentialWorker = staffDao.getStaffDetail(workerId);
        } catch (Exception e) {
            throw new ValidateException("获取用员工列表异常");
        }
        return residentialWorker;
    }

    /**
     * 更新员工信息
     *
     * @param residentialWorker
     */
    public void updateWorker(ResidentialWorker residentialWorker) {
        try {
            staffDao.updateWorker(residentialWorker);
        } catch (Exception e) {
            throw new ValidateException("更新用员工异常");
        }
    }

    /**
     * 新增员工信息
     *
     * @param residentialWorker
     */
    public void addWork(ResidentialWorker residentialWorker) {
        try {
            staffDao.addWorker(residentialWorker);
        } catch (Exception e) {
            throw new ValidateException("新增用员工异常");
        }
    }

}
